package com.woniu.tank.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import com.woniu.tank.boom.Boom;
import com.woniu.tank.bullet.BadBullet;
import com.woniu.tank.bullet.Bullet;
import com.woniu.tank.bullet.GoodBullet;
import com.woniu.tank.map.GameMap;
import com.woniu.tank.tank.BadTank;
import com.woniu.tank.tank.GoodTank;

public class GamePanel extends JPanel implements Runnable {
	//实例化一个地图
	public static int[][] map= new GameMap().map_01;
	//地图
	private Image image;
	//自己的坦克
	private Image goodtank;
	//敌方坦克
	private Image badtank;
	//子弹
	private Image bullet;
	//星星
	private Image star;
	//爆炸
	private Image boom;

	GoodTank good = new GoodTank(11,22,this);
	BadTank bad = new BadTank(1,1,this);
	//创建好子弹容器
	public static Vector<GoodBullet> gbbullet = new Vector<GoodBullet>();
	//创建坏子弹容器
	public static Vector<BadBullet> bbullet = new  Vector<BadBullet>();
	//创建坦克容器
	public static Vector<BadTank> tanktk= new Vector<BadTank>();
	//创建自己的坦克容器
	public static Vector<GoodTank> gttank= new Vector<GoodTank>();
	//爆炸容器
	public static Vector<Boom> boomtk = new Vector<Boom>();

	public GamePanel(){
		this.setLayout(null);
		//设置布局的 背景颜色
		this.setBackground(Color.black);
		//通过路径读取图片
		File file = new File("image/com.bmp");
		File tk = new File("image/Tank.com.bmp");
		File bdtk=new File("image/enemy.bmp");
		File bt = new File("image/buule.png");
		File superstar = new File("image/star.bmp");
		File flboom = new File("image/boom1.png");

		try {
			image = ImageIO.read(file);
			goodtank=ImageIO.read(tk);
			badtank = ImageIO.read(bdtk);
			bullet = ImageIO.read(bt);
			star = ImageIO.read(superstar);
			boom = ImageIO.read(flboom);
			//实例媒体跟踪器，并加载所有的图片
			MediaTracker mt = new MediaTracker(this);
			mt.addImage(image,1);
			mt.addImage(goodtank,2);
			mt.addImage(badtank, 3);
			mt.addImage(bullet, 4);
			mt.addImage(star, 5);
			mt.addImage(boom, 6);
			mt.waitForAll();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setFocusable(true);
		//在面板上面设置监听器，把按键事件传递给move()方法
		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				good.move(e,map);
			}

		} );
		new Thread(this).start();
		new Thread(bad).start();
		//		new Thread(good).start();
		tanktk.add(bad);
		gttank.add(good);


	}
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		this.drawMap(g);
		this.drawGood(g);
		this.drawBad(g);
		this.drawBullet(g);
		this.drawBooms(g);
	}
	//绘制好坦克
	private void drawGood(Graphics g){
		g.drawString("自己:"+gttank.size(), 100, 10);
		for(int i=0;i<gttank.size();i++){
			GoodTank gt = gttank.get(i);
			gt.drawGoodtank(g, goodtank, this, image);
		}
	}
	//绘制敌方坦克
	private void drawBad(Graphics g){
		g.drawString("敌军:"+tanktk.size(), 100, 20);
		for(int i=0;i<tanktk.size();i++){			
			BadTank bt = tanktk.get(i);			
			bt.draw(g, this, star, badtank, image);
		}
	}
	//子弹
	private void drawBullet(Graphics g){
		g.drawString("炮弹:"+gbbullet.size(), 50, 10);
		g.drawString("坏蛋:"+bbullet.size(), 50, 20);
		for(int i=0;i<gbbullet.size();i++){
			Bullet gb = gbbullet.get(i);		
			gb.drawBullet(g, this, bullet);
		}
		for(int i=0;i<bbullet.size();i++){
			Bullet gb = bbullet.get(i);		
			gb.drawBullet(g, this, bullet);
		}
	}
	//爆炸
	private void drawBooms(Graphics g){
		for(int i=0;i<boomtk.size();i++){
			boomtk.get(i).drawBoom(g, this, boom);
		}
	}
	//绘制地图
	public void drawMap(Graphics g){
		for(int i = 0; i < map.length ; i ++){
			for(int j = 0; j< map[0].length; j ++){
				if(map[i][j] == 1){
					g.drawImage(image, j << 5, i << 5, (j+1)<<5, (i+1)<<5, 32, 0, 64, 32,this);
				}if(map[i][j] == 2){
					g.drawImage(image, j << 5, i << 5, (j+1)<<5, (i+1)<<5, 0, 0, 32, 32,this);
				}if(map[i][j] == 3){
					g.drawImage(image, j << 5, i << 5, (j+1)<<5, (i+1)<<5, 64, 0, 96, 32,this);
				}if(map[i][j] == 4){
					g.drawImage(image, j << 5, i << 5, (j+1)<<5, (i+1)<<5, 96, 0, 128, 32,this);					
				}if(map[i][j] == 5){
					g.drawImage(image, j << 5, i << 5, (j+1)<<5, (i+1)<<5, 160, 0, 192, 32,this);
				}
			}
		}
	}


	@Override
	public void run() {
		while(true){
			this.repaint();			
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
